// ignore_for_file: camel_case_types
import 'package:bitsdojo_window/bitsdojo_window.dart';
import 'package:flutter/material.dart';
import 'package:flutter_side_menu/flutter_side_menu.dart';
import 'package:package_info_plus/package_info_plus.dart';

class SizeMenuNav extends StatefulWidget {
  final void Function(int index) onTapSide;
  const SizeMenuNav({super.key, required this.onTapSide});

  @override
  State<SizeMenuNav> createState() => _SizeMenuNavState();
}

class sideItem {
  final String title;
  final IconData icon;
  sideItem(this.title, this.icon);
}

class _SizeMenuNavState extends State<SizeMenuNav> {
  int activeIndex = 0;
  double sidebarWidth = 180;
  String version = '';

  void getVersion() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    setState(() {
      version = packageInfo.version;
    });
  }

  @override
  void initState() {
    super.initState();
    getVersion();
  }

  @override
  Widget build(BuildContext context) {
    List<sideItem> sideItems = [
      sideItem('首页', Icons.home),
      sideItem('最新', Icons.new_label),
      sideItem('波奇壁纸', Icons.layers),
      sideItem('wallhaven', Icons.import_contacts),
      sideItem('Loveanimer壁纸', Icons.real_estate_agent),
      sideItem('萌虎壁纸', Icons.support_agent),
      sideItem('个人中心', Icons.account_box),
      sideItem('工具箱', Icons.build),
    ];
    final theme = Theme.of(context).colorScheme;

    return SideMenu(
      mode: SideMenuMode.open,
      maxWidth: sidebarWidth,
      backgroundColor: theme.primaryContainer,
      hasResizer: true,
      hasResizerToggle: true,
      resizerToggleData: ResizerToggleData(
        iconColor: theme.primary,
        opacity: 1,
        iconSize: 25,
      ),
      builder: (data) => SideMenuData(
        header: Column(
          spacing: 10,
          children: [
            SizedBox(height: 30, child: MoveWindow()),
            FittedBox(
              fit: BoxFit.scaleDown,
              child: Padding(
                padding: const EdgeInsets.only(left: 10, right: 10),
                child: Image.asset(
                  'lib/assets/images/icon.png',
                  width: 80,
                  height: 80,
                ),
              ),
            ),
            FittedBox(
              fit: BoxFit.scaleDown,
              child: Padding(
                padding: const EdgeInsets.only(left: 10, right: 10),
                child: Text(
                  '波奇壁纸',
                  style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ),
            // 分割线
            const Divider(
              color: Colors.grey,
              height: 10,
              thickness: 1,
              indent: 10,
              endIndent: 10,
            ),
          ],
        ),
        items: [
          for (var item in sideItems)
            SideMenuItemDataTile(
              hasSelectedLine: true,
              borderRadius: BorderRadius.all(Radius.circular(5)),
              isSelected: activeIndex == sideItems.indexOf(item),
              tooltip:
                  data.currentWidth <= data.minWidth + 30 ? item.title : null,
              titleStyle: TextStyle(
                fontSize: 15,
              ),
              selectedTitleStyle: TextStyle(
                fontSize: 15,
                color: theme.primary,
              ),
              onTap: () {
                setState(() {
                  activeIndex = sideItems.indexOf(item);
                });
                widget.onTapSide(sideItems.indexOf(item));
              },
              title: item.title,
              icon: Icon(item.icon),
            ),
        ],
        footer: FittedBox(
          fit: BoxFit.scaleDown,
          child: Padding(
            padding: const EdgeInsets.all(5),
            child: Text(
              'v$version',
              style: TextStyle(
                fontSize: 13,
              ),
            ),
          ),
        ),
      ),
    );
  }
}
